package com.chenfeng.ego.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.chenfeng.ego.commons.pojo.EasyUIDataGrid;
import com.chenfeng.ego.commons.pojo.EgoResult;
import com.chenfeng.ego.pojo.TbItem;
import com.chenfeng.ego.service.TBItemService;

@Controller
public class TbItemController
{
	@Resource
	TBItemService tbItemService;
	//通配符 传过来的值跟返回值一致
	@RequestMapping("{page}")
	public String showPage(@PathVariable String page)
	{
		return page;
	}
	//编辑框
	@RequestMapping("rest/page/item-edit")
	public String uodateTbitem()
	{
		return "item-edit";
	}

	// 显示商品列表
	@RequestMapping("item/list")
	@ResponseBody
	public EasyUIDataGrid show(int page, int rows)
	{
		return this.tbItemService.showTbItem(page, rows);
	}

	// 自己写的删除 上架 下架
	/*
	 * //删除
	 * 
	 * @RequestMapping("rest/item/delete")
	 * 
	 * @ResponseBody public Map deleteTbitem(String ids) {
	 * System.out.println(ids); String id[] = ids.split(","); int status =
	 * this.tbItemService.deleteTbItem(id); Map data = new
	 * HashMap<String,Object>(); data.put("status", status); return data; } //下架
	 * 
	 * @RequestMapping("rest/item/instock")
	 * 
	 * @ResponseBody public Map instockTbitem(String ids) {
	 * System.out.println(ids); String id[] = ids.split(","); int status =
	 * this.tbItemService.instockTbItem(id); Map data = new
	 * HashMap<String,Object>(); data.put("status", status); return data; } //上架
	 * 
	 * @RequestMapping("rest/item/reshelf")
	 * 
	 * @ResponseBody public Map resheltkTbitem(String ids) {
	 * System.out.println(ids); String id[] = ids.split(","); int status =
	 * this.tbItemService.reshelfTbItem(id); Map data = new
	 * HashMap<String,Object>(); data.put("status", status); return data; }
	 */
	//更新状态 状态：status中的123分别代表上架 下架 删除 
	@RequestMapping("rest/item/{type}")
	@ResponseBody
	public EgoResult updataStatus(@PathVariable String type, String ids)
	{
		EgoResult egoResult = new EgoResult();
		Byte status = null;
		if ("delete".equalsIgnoreCase(type))
		{
			status = (byte) 3;
		} else if ("instock".equalsIgnoreCase(type))
		{
			status = (byte) 2;
		} else if ("reshelf".equalsIgnoreCase(type))
		{
			status = (byte) 1;
		}
		try
		{
			tbItemService.upStatus(ids, status);
			egoResult.setStatus(200);
		} catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			egoResult.setStatus(400);
		}
		return egoResult;
	}
	//商品的保存  以及参数的保存
	@RequestMapping("item/save")
	@ResponseBody
	public EgoResult insertItemAndParam(TbItem tbItem ,String itemParams)
	{
		EgoResult eg = new EgoResult();
		int result;
		try
		{
			result = tbItemService.insertByItemAndParams(tbItem,itemParams);
			if(result>1)
			{
				eg.setStatus(200);
			}
		} catch (Exception e)
		{
				eg.setStatus(400);
			e.printStackTrace();
		}
		return eg;
	}

}
